<template>
	<view class="content">
		<cu-custom bgColor="bg-gradual-blue" :isBack="true">
			<block slot="content">注册</block>
		</cu-custom>
		<view class="cu-form-group margin-top">
			<view class="title">账号：</view>
			<input placeholder="请输入账号" v-model="username" name="input"></input>
		</view>
		<view class="cu-form-group">
			<view class="title">密码：</view>
			<input placeholder="请输入密码" v-model="password" name="input"></input>
		</view>
		<view class="cu-form-group">
			<view class="title">确认密码：</view>
			<input placeholder="请确认密码" v-model="confirmPassword" name="input"></input>
		</view>
		<view class="btn-row">
			<button class="cu-btn block bg-blue margin-tb-sm lg" @click="register">注册</button>
		</view>
	</view>
</template>

<script>
	import Request from '../../lib/request.js'
	
	export default {
		data() {
			return {
				username: '',
				password: '',
				confirmPassword: '',
				tapTime: ''
			}
		},
		methods: {
			register() {
				let that = this;
				// 防止两次点击操作间隔太快
				var nowTime = new Date();
				if (nowTime - that.tapTime < 1000) {
				  return;
				}
				
				if (this.username.length < 2) {
					uni.showToast({
						icon: 'none',
						title: '账号最短为 2 个字符'
					});
					return;
				}
				if (this.password.length < 6) {
					uni.showToast({
						icon: 'none',
						title: '密码最短为 6 个字符'
					});
					return;
				}
				if (this.password !== this.confirmPassword) {
					uni.showToast({
						icon: 'none',
						title: '两次密码输入不一致'
					});
					return;
				}

				let data = {
					username: this.username,
					password: this.password,
					confirmPassword:this.confirmPassword
				}
				Request.httpRequest({url:'User/userRegister',method:'POST'}, data).then(res => {
					if(res.code == 1){
						uni.showToast({
							title: '注册成功,3秒后自动跳转登录',
							icon: "none",
							duration: 3000,
							success:function(){
								// 注册成功跳转登录
								setTimeout(()=>{
									uni.navigateBack({
										delta:1
									})
								},3000)
							}
						})
					}else{
						uni.showModal({
							content: res.msg,
							showCancel: false
						})
						console.log('注册失败', e);
					}
                })

				that.tapTime = nowTime;
			}
		}
	}
</script>

<style>
	.btn-row{
		position: fixed;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		bottom: 10px;
		width: 100%;
		padding: 0 2%;
	}
	.btn-row button{
		width: 98%;
	}
</style>
